package com.bst.etl.utils;

import com.bst.common.constant.JobConstant;
import com.bst.common.core.domain.AjaxResult;
import com.bst.common.vo.DimVO;
import com.bst.etl.vo.DmQueryIndexVO;
import com.bst.md.domain.MdIndex;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;

public class IndexQueryUtil {

    public static final DecimalFormat FLOAT_FORMAT = new DecimalFormat("#.###");
    public static final DecimalFormat PERCENT_FORMAT = new DecimalFormat("#.##%");

    /**
     * 方便计算月同比
     * @return
     */
    public static Map<Integer,Map<String,Object>> dataToMonthMap(List<Map<String,Object>> list) {
        String day;Integer month;
        Map<Integer,Map<String,Object>> dataMap=new LinkedHashMap<>();
        if(list!=null) {
            for (Map<String, Object> map : list) {
                day = map.get("ID_DIM_DAY_OCCUR").toString();
                month = Integer.parseInt(day.substring(4,6));
                dataMap.put(month,map);
            }
        }
        return dataMap;
    }

    public static Object getResult(AjaxResult result, MdIndex index, Object def) {
        if("0".equals(result.get(AjaxResult.CODE_TAG)+"")) {
            List<Map<String,Object>> list =(List<Map<String,Object>>) result.get(AjaxResult.DATA_TAG);
            if(list!=null && list.size()>0) {
                return resultHandler(list.get(0).get(index.getIdIndex()),index.getSdValtp());
            }
        }
        return def;
    }

    public static Map<String,Object> singleDimResultHandler(AjaxResult result) {
        List<Map<String,Object>> list = (List<Map<String,Object>>)result.get(AjaxResult.DATA_TAG);
        Map<String,Object> dataMap = new HashMap<>();
        if(list!=null) {
            for (Map<String, Object> map : list) {
                for(Map.Entry<String, Object> entry : map.entrySet()) {
                    if(entry.getKey().startsWith("IDX_")) {
                        dataMap.put(entry.getKey(),entry.getValue());
                    }
                }
            }
        }
        return dataMap;
    }

    public static Object resultHandler(Object value,Object value2,String sdValtp) {
        if(value==null || value2==null || "0".equals(value2)) {
            return 0;
        }
        BigDecimal v = new BigDecimal(value.toString()).divide(new BigDecimal(value2.toString()),4,BigDecimal.ROUND_HALF_UP);
        return resultHandler(v.toString(),sdValtp);
    }

    public static Object resultHandler(Object value, String sdValtp) {
        if(value==null) return 0;
        value = Double.parseDouble(value.toString());
        if("scale".equals(sdValtp)) {
            value = "1:"+FLOAT_FORMAT.format(value);
        } else if("percent".equals(sdValtp)) { //百分比
            value = (Double)value*100;
            value = FLOAT_FORMAT.format(value);
        }  else if("permillage".equals(sdValtp)) { //千分比
            value = (Double)value*1000;
            value = FLOAT_FORMAT.format(value);
        } else if("value_float".equals(sdValtp)) {
            value = FLOAT_FORMAT.format(value);
        }else if("value_int".equals(sdValtp)) {
            value = ((Double)value).intValue();
        }
        return value;
    }

    public static Object dataHandler(Object value) {
        if(value!=null && value.toString().contains("%")) {
            String newValue = value.toString().replace("%","");
            return new BigDecimal(newValue).divide(new BigDecimal("100"));
        }
        return value;
    }

    public static List<DmQueryIndexVO> singleIndexToList(String idIndex) {
        List<DmQueryIndexVO> idIndexList = new ArrayList<>();
        idIndexList.add(new DmQueryIndexVO(idIndex));
        return idIndexList;
    }

    public static List<DmQueryIndexVO> moreIndexToList(String[] idIndexs) {
        List<DmQueryIndexVO> list = new ArrayList<>();
        for (String idIndex : idIndexs) {
            list.add(new DmQueryIndexVO(idIndex));
        }
        return list;
    }

    public static List<DimVO> getDimVOList(String idPubfld) {
        List<DimVO> list = new ArrayList<>();
        list.add(new DimVO(idPubfld,null));
        return list;
    }

    //1 小数,2 整数,3 百分数,4 比值
    public static String getValueFld(String euValtp) {

        if(JobConstant.VALUE_FLOAT.equals(euValtp)) { //小数插入fl
            return "VALUE_FLOAT";
        } else if(JobConstant.VALUE_INT.equals(euValtp)) { //小数插入fl
            return "VALUE_INT";
        }
        return null;
    }

    /**
     * sql别名处理，比如 id_dept as id_dim_dept 返回 id_dim_dept
     * @return
     */
    public static String sqlAliasHandler(String fields){
        String[] fieldArr = fields.split(",");
        String newFlds="";
        String onefieldArr[];
        for (String fld : fieldArr) {
            onefieldArr = fld.split(" as ");
            if(onefieldArr.length==2) {
                newFlds += onefieldArr[1]+",";
            } else {
                onefieldArr = fld.split(" ");
                if(onefieldArr.length==2) {
                    newFlds += onefieldArr[1]+",";
                } else {
                    newFlds += fld;
                }
            }
        }
        return newFlds.substring(0,newFlds.length()-1);
    }

    public static String getDateFlter(String dateType) {
        switch (dateType) {
            case "M":
                return "dd.mm";
            case "Q":
                return "dd.qq";
            case "Y":
                return "dd.yy";
            default:
                return "dm.id_dim_day_occur";
        }
    }

    public static String getIndexTypeDes(Integer indexType){
        String des;
        switch (indexType) {
            case 0:
                des = "原";
                break;
            case 1:
                des = "复";
                break;
            case 2:
                des = "子";
                break;
            default:
                des="";
                break;
        }
        return des;
    }

    public static void main(String[] args) {
        System.out.println(FLOAT_FORMAT.format(1.00223));
    }
}
